﻿using System.Text;
using Sanelib.Common.SystemSettings;

namespace Sanelib.CodeMaker.Generators
{
    public class CshtmlGenerator
    {
        public static void Generate(DatabaseTableSchema form)
        {
            var sb = new StringBuilder();
            DefaultHeader(form, sb);
            System.IO.File.WriteAllText(Globals.CreateOrGetCustomPath("\\Views\\" + form.TableName + "\\") + "List_Generated.cshtml", sb.ToString());
        }
        
        private static void DefaultHeader(DatabaseTableSchema form, StringBuilder sb)
        {
            sb.AppendLine("@using WebApp.Controllers");
            sb.AppendLine("@using WebApp.Extensions");
            sb.AppendLine("//Add Api Model");
            sb.AppendLine("");
            sb.AppendLine("@{");
            sb.AppendLine("\tViewBag.CurrentPage = WebApp.Common.Page."+ form.TableName +";");
            sb.AppendLine("\tLayout = \"~/Views/Shared/_Layout.cshtml;");
            sb.AppendLine("}");
            sb.AppendLine(""); 
            sb.AppendLine("@section Scripts{");
            sb.AppendLine("<script type=\"text/javascript\">");
            sb.AppendLine("\trequire(['" + form.TableName + "/Index'], function (api) {");
            sb.AppendLine("\t$(function () { api.exec(); });");
            sb.AppendLine("});");
            sb.AppendLine("</script>");
        }
    }
}
